package com.yangzb.ppt;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.common.usermodel.fonts.FontGroup;
import org.apache.poi.common.usermodel.fonts.FontInfo;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.sl.usermodel.Placeholder;
import org.apache.poi.xslf.usermodel.*;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;

import java.awt.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class Ppt {
    public static void main(String[] args) {
//        try {
//            FileInputStream fis = new FileInputStream("example.ppt"); // 打开ppt文件流
//            HSLFSlideShow ppt = new HSLFSlideShow(fis); // 创建HSLFSlideShow对象
//            fis.close(); // 关闭文件流
//            // 处理pptX文件
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        try {
            ZipSecureFile.setMaxFileCount(1000000);
            FileInputStream fis = new FileInputStream("/Users/yzb/Downloads/重庆政企销售提能方案-V1.0.pptx"); // 打开pptX文件流
            XMLSlideShow pptx1 = new XMLSlideShow(fis); // 创建XMLSlideShow对象


            FileInputStream fis2 = new FileInputStream("/Users/yzb/Downloads/基于AI大模型实现商机挖掘与方案生成.pptx"); // 打开pptX文件流
            XMLSlideShow pptx2 = new XMLSlideShow(fis2); // 创建XMLSlideShow对象
            Dimension pageSize = pptx1.getPageSize();
//            XSLFSlideLayout layout1 = pptx1.getSlides()
//            XSLFSlideLayout layout2 = pptx2.getSlideMasters().get(0).getLayout(SlideLayout.TITLE_AND_CONTENT);


            FileInputStream fis3 = new FileInputStream("/Users/yzb/Downloads/999.pptx"); // 打开pptX文件流
            XMLSlideShow pptx3 = new XMLSlideShow(fis3); // 创建XMLSlideShow对象
            pptx3.setPageSize(pageSize);
            int size = pptx3.getSlides().size();
            if (size > 0) {
                for (int i = 0; i < size; i++) {
                    pptx3.removeSlide(i);
                }
            }

            List<XSLFSlide> sourceSlides = new ArrayList<>();
            for (XSLFSlide slide : pptx1.getSlides()) {
                sourceSlides.add(slide);
            }
            for (XSLFSlide slide : pptx2.getSlides()) {
                sourceSlides.add(slide);
            }
            XSLFSlideMaster xSLFSlideMaster = pptx3.getSlideMasters().get(0);
            XSLFSlideLayout layout = xSLFSlideMaster.getLayout("8_仅标题空内容");
            XSLFSimpleShape xslfSimpleShape = layout.getPlaceholder(Placeholder.TITLE);
            XSLFAutoShape xslfAutoShapeM = (XSLFAutoShape) xslfSimpleShape;
            XSLFTextRun xslfTextRunS = null;
            s:
            for (XSLFTextParagraph textParagraph : xslfAutoShapeM.getTextParagraphs()) {
                for (XSLFTextRun run : textParagraph.getTextRuns()) {
                    xslfTextRunS = run;
                    break s;
                }
            }

            for (XSLFSlide sourceSlide : sourceSlides) {
                XSLFSlide nslide1 = pptx3.createSlide(layout);
                CopySlideDemo.copySlideContent(sourceSlide, nslide1);
            }


            for (XSLFSlide slide : pptx3.getSlides()) {
                XSLFShape placeholder = slide.getPlaceholder(Placeholder.TITLE);
                XSLFAutoShape xslfAutoShape = (XSLFAutoShape) placeholder;
                if (xslfAutoShape != null) {
                    for (XSLFTextParagraph paragraph : xslfAutoShape.getTextParagraphs()) {
                        // 获取段落中的文本运行
                        for (XSLFTextRun run : paragraph.getTextRuns()) {
                            // 设置文本颜色
//                        run.setFontColor(Color.RED);
                            copy(run, xslfTextRunS);

                        }
                    }
                }


            }

            fis.close(); // 关闭文件流
            fis2.close(); // 关闭文件流
            FileOutputStream out = new FileOutputStream("/Users/yzb/Downloads/3.pptx");
            pptx3.write(out);
            out.close();
            fis3.close();

            // 处理pptX文件
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void copy(XSLFTextRun newRun, XSLFTextRun run) {
        newRun.setFontColor(run.getFontColor());  // 复制字体颜色
        newRun.setFontSize(run.getFontSize());    // 复制字体大小
        newRun.setFontFamily(run.getFontFamily()); // 复制字体名称
        newRun.setBold(run.isBold());         // 复制是否加粗
        newRun.setItalic(run.isItalic());     // 复制是否斜体
        newRun.setUnderlined(run.isUnderlined());
    }
}
